// pages/complex/complex.js
const db = wx.cloud.database()
const _ = db.command
const $ = _.aggregate
const commoditiesCollection = db.collection('commodities')


Page({

  /**
   * 页面的初始数据
   */
  data: {
    


  },


 
  onShow:function(){
    commoditiesCollection.get().then(res =>{
      this.setData({
        commodities: res.data
      })
    })
  },
  lt:function(){
    commoditiesCollection.where({
      
      goods: _.all([
        _.elemMatch({
         
          currentPrice: _.lte(500)
          
        }),
        _.elemMatch({
          
        }),
      ]), 
        

      
    }).get().then(res =>{
      console.log(res);
      this.setData({
        commodities: res.data
      })
    })
  },



  gt:function(event){
    commoditiesCollection.where({
      
      goods: _.all([
        _.elemMatch({
         
          currentPrice: _.gte(500)
          
        }),
        _.elemMatch({
          
        }),
      ]), 
        

      
    }).get().then(res =>{
      console.log(res);
      this.setData({
        commodities: res.data
      })
    })
  },
  bt:function(){
    commoditiesCollection.orderBy(
      
     "typeID","asc"
    
    ).get().then(res =>{
      this.setData({
        commodities: res.data
      })
    })
  },
  ct:function(event){
    commoditiesCollection.orderBy(
      
     "typeID","desc"
    
    ).get().then(res =>{
      this.setData({
        commodities: res.data
      })
    })
  },
  searchF: function (e) {
    console.log(e.detail.value)
    db.collection("commodities").where({
        'goods.goodsTitle': db.RegExp({
          regexp: e.detail.value,
          options: 'i',
        })
      }).get().then(res => {
        // console.log('success', res)
        console.log(res.data[0].goods)
        this.setData({
          commodities: res.data[0]
        })
        console.log(this.data.goodList)
        wx.hideLoading();
      })
      .catch(err => {
        console.log('fail', err)
      })
    },
  //at:function(event){
    //commoditiesCollection.aggregate()
    // .match({
     //  _id:'5c936e0dcf6db9473888b8dd',
    // })
    // .project({
    //   goods:$.filter({
     //    input:'$goods',
       //  as:'item',
     //    cond: $.eq(['$$item.goodsID','1'])
     //  })
   //  })
   //  .unwind('$goods')

   // .end().then(res =>{
   //   console.log(res);
    //  this.setData({
     //   commodities: res.data
    //  })
  //  })
    
 // }
  
 dt:function(event){
    commoditiesCollection.where({
      
     wares:"old"

      
    }).get().then(res =>{
      console.log(res);
      this.setData({
        commodities: res.data
      })
    })
  },

  et:function(event){
    commoditiesCollection.where({
      
      wares:"new"
        

      
    }).get().then(res =>{
      console.log(res);
      this.setData({
        commodities: res.data
      })
    })
  },

})